﻿package app.demo.MyTouchApp{
	   
	import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.*;
	import fl.core.*;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import flash.utils.getTimer;
	import flash.utils.Timer;
	import fl.transitions.easing.*;
	import flash.media.*;
	
	  public class topicGame extends MovieClip {
		  
		private var color:uint = 0xff0000; // color of lines
        private var thickness:Number = 30; // thickness of lines
        private var blobs:Array = new Array(); // blobs we are currently interacting with
        private var lines:Array = new Array(); // array to hold the lines
		private var numButtonWidth:Number = 100;
		private var numButtonHeight:Number = 50;
		
		var movie1:MovieClip = new MovieClip();
		var myTextFormat1:TextFormat = new TextFormat();
		
		private var avatars:Array = ["av11.png", "av22.png", "av33.png", "av44.png","av55.png","av66.png","av77.png","av88.png","av99.png","av100.png"];
		
		private var images:Array = ["darkBackground.png","av1.jpg", "transportation3.png","mute.png","unmute.png"];
		
		private var pics:Array = ["1.png","2.png","6.png","8.png","9.png",
								  "11.png","train.png","bike.png","111.png","222.png","333.png", "1000.png","1100.png"];
		
		private var names:Array = ["Boat","Car","Motorbike","Bus","Airplane","Helicopter","Train","Bike","Frying pan","Telephone","Alarm Clock",
								   "Chocolate bar","Cheese"];
								   
		
		var position:Array=new Array(pics.length*2);
		
		var positionFixed:Array=new Array(16);
		
		var iLoaders:Array = new Array (pics.length); 
		
		var allLabels:Array = new Array(8);
		
		var imageBack:Loader = new Loader();
		var avatar:Loader = new Loader();
		var topicL:Label = new Label();
		var player:Label = new Label();
		var mainLoader:Loader = new Loader();
		var mainLoadern:Loader = new Loader();
		var mainLoader1:Loader = new Loader();
		var mainLoader2:Loader = new Loader();
		var topic:Loader = new Loader();
		var noSound:Boolean=false;
		
		public var buttonBack:Button;
		public var buttonExit:Button;
		public var buttonDone:Button;
		var skipp:Button = new Button();
		var skipp1:Button = new Button();
		var skip2:Boolean = false;
		var backTip:Button;
		
		var square:Sprite = new Sprite();
		
		var myMaster:master;
		
		var errorSound:Sound;
		var correctSound:Sound;
		var mute:Loader  = new Loader();
		
		var once:Boolean=true;
		
		var right:Loader = new Loader();
		var wrong:Loader = new Loader();
		
		var bmpimages:Array = new Array();
		
		public var bringToFront:Boolean = true;
		public var mouseSelection = false;
		public var xdist:Number;
		public var ydist:Number;
		public var distance:Number;
		private var oldX:Number = 0;
		private var oldY:Number = 0;		
		public var lastClick:Number = 0;
		public var clickRadius:Number = 50;		
		
		var enable:Boolean=false;
		var pleft:int=8;
		var pdone:int=0;
		var pdone2:int=0;
		var leftLabel:Label = new Label();
		
		var blank1:Sprite = new Sprite();
		var blank1t:Label = new Label();
		var blank2:Sprite = new Sprite();
		var blank2t:Label = new Label();
		var blank3:Sprite = new Sprite();
		var blank3t:Label = new Label();
		var blank4:Sprite = new Sprite();
		var blank4t:Label = new Label();
		var blank5:Sprite = new Sprite();
		var blank5t:Label = new Label();
		var blank6:Sprite = new Sprite();
		var blank6t:Label = new Label();
		var blank7:Sprite = new Sprite();
		var blank7t:Label = new Label();
		var blank8:Sprite = new Sprite();
		var blank8t:Label = new Label();
		
		var gIndex:int=100;
		var backposition:int=100;
		var myTimerC:Timer = new Timer(2000);
		var myTimerI:Timer = new Timer(2000);
		var timerLast:Timer = new Timer(2000);
		
		var bigName:Label=new Label();
		
		public function topicGame(m:master):void {
			
			TUIO.init(this,'localhost',3000,'',true);
			trace("MyTouchApp Initialized");
			myMaster=m;
			addChild(movie1);
			
			//addEventListener(TouchEvent.MOUSE_UP, upEvent, false, 0, true); // run when finger up
       		addEventListener(Event.ENTER_FRAME, updater, false, 0, true); // keep running
			
			imageBack.load(new URLRequest(images[0]));
			imageBack.scaleX=1.4;
			//imageBack.scaleY=0.8;
			movie1.addChild(imageBack);
			
			mute.load(new URLRequest(images[4]));
			mute.name='unmute';
			mute.x=910;
			mute.y=555;
			mute.scaleX=0.3;
			mute.scaleY=0.3;
			mute.addEventListener(TouchEvent.MOUSE_DOWN,muteMethod);
			movie1.addChild(mute);
			
			myTextFormat1.font = "Kristen ITC";
			myTextFormat1.size = 70;
			
			topic.load(new URLRequest(images[2]));
			topic.x=250;
			topic.y=-30;
			topic.scaleX=1.1;
			topic.scaleY=1.1;
			movie1.addChild(topic);
			
			buttonBack = new Button();
			buttonBack.label="BACK";
			buttonBack.width=100;
			buttonBack.height=40;
			//buttonBack.emphasized=true;
			buttonBack.addEventListener(TouchEvent.MOUSE_DOWN,backMethod);
			movie1.addChild(buttonBack);
			
			buttonExit = new Button;
			buttonExit.x=900;
			buttonExit.y=650;
			buttonExit.width=100;
			buttonExit.height=40;
			buttonExit.label="EXIT";
			buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
			movie1.addChild(buttonExit);
			
			backTip = new Button;
			backTip.x=50;
			backTip.y=650;
			backTip.width=100;
			backTip.height=40;
			backTip.label="Show Tip";
			backTip.addEventListener(TouchEvent.MOUSE_DOWN,backTips);
			backTip.alpha=0.5;
			backTip.enabled=false;
			movie1.addChild(backTip);
			
			avatar.load(new URLRequest(avatars[myMaster.getTheIndex()]));
			avatar.x=860;
			avatar.y=20;
			avatar.scaleX=1.1;
			avatar.scaleY=1.1;
			//avatar.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod1);
			movie1.addChild(avatar);
		
			var myTextFormat2:TextFormat = new TextFormat();
			myTextFormat2.font = "Kristen ITC";
			myTextFormat2.size = 20;
			myTextFormat2.color = 0xffffff;
			
			player.text = myMaster.getTheText();
			player.width=300;
			player.height=40;
			player.x=895; 
			player.y=140;  
			player.visible=true;
			player.setStyle("textFormat", myTextFormat2);
			movie1.addChild(player);
		

			var url = new URLRequest("example4.swf"); 
			mainLoader.load(url);
			mainLoader.y=470;  //10
			mainLoader.x=20;  //852
			mainLoader.addEventListener(TouchEvent.MOUSE_DOWN,moveTip);
			movie1.addChild(mainLoader);
			
			
			skipp.label="Skip All Tips";
			//skipp.addEventListener(TouchEvent.MOUSE_DOWN, skip3); 
			skipp.y=617;  //146
			skipp.x=38;
			skipp.width=130;
			skipp.name="hhh";
			skipp.visible=false;
			movie1.addChild(skipp);
			
			//mainLoader= new Loader();
			/*
			var url1 = new URLRequest("example2.swf"); 
			mainLoader1.load(url1);
			mainLoader1.y=380;  //190
			mainLoader1.x=852;
			mainLoader1.visible=false;
			*/
			//movie1.addChild(mainLoader1);
			
			
			skipp1.label="Skip All Tips";
			skipp1.addEventListener(TouchEvent.MOUSE_DOWN, skip4); 
			skipp1.y=514;  //327
			skipp1.x=870;
			skipp1.width=130;
			skipp1.name="hhh2";
			skipp1.visible=false;
			//movie1.addChild(skipp1);
			
			//mainLoader= new Loader();
			var url2 = new URLRequest("example3.swf"); 
			mainLoader2.load(url2);
			mainLoader2.y=537;  //350
			mainLoader2.x=852;
			mainLoader2.visible=false;
			
			
			square.graphics.lineStyle(3,0xffffff);
			square.graphics.drawRect(0,0,200,200);
			square.x=412;
			square.y=130;
			movie1.addChild(square);
			
			topicL.text = "Drag Here";
			topicL.width=200;
			topicL.height=50;
			topicL.x=460;
			topicL.y=200;
			topicL.visible=true;
			topicL.alpha=0.5;
			topicL.setStyle("textFormat", myTextFormat2);
			movie1.addChild(topicL);
			
			var myTextFormat3:TextFormat = new TextFormat();
			myTextFormat3.font = "Kristen ITC";
			myTextFormat3.size = 30;
			myTextFormat3.color = 0xffffff;
			
			bigName.text ="";
			bigName.width=230;
			bigName.height=50;
			bigName.x=420;
			bigName.y=340;
			bigName.visible=false;
			bigName.setStyle("textFormat", myTextFormat3);
		
			blank1.graphics.lineStyle(3,0xffffff);
			blank1.graphics.drawRect(0,0,90,90);
			blank1.x=20;
			blank1.y=190;
			movie1.addChild(blank1);
		
			
			blank1t.text = "1";
			blank1t.width=90;
			blank1t.height=50;
			blank1t.x=55;
			blank1t.y=220;
			blank1t.visible=true;
			blank1t.alpha=0.5;
			blank1t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank1t);
			
			blank2.graphics.lineStyle(3,0xffffff);
			blank2.graphics.drawRect(0,0,90,90);
			blank2.x=130;
			blank2.y=190;
			movie1.addChild(blank2);
			
			blank2t.text = "2";
			blank2t.width=90;
			blank2t.height=50;
			blank2t.x=165;
			blank2t.y=220;
			blank2t.visible=true;
			blank2t.alpha=0.5;
			blank2t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank2t);
			
			blank3.graphics.lineStyle(3,0xffffff);
			blank3.graphics.drawRect(0,0,90,90);
			blank3.x=20;
			blank3.y=320;
			movie1.addChild(blank3);
			
			blank3t.text = "3";
			blank3t.width=128;
			blank3t.height=50;
			blank3t.x=55;
			blank3t.y=350;
			blank3t.visible=true;
			blank3t.alpha=0.5;
			blank3t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank3t);
			
			blank4.graphics.lineStyle(3,0xffffff);
			blank4.graphics.drawRect(0,0,90,90);
			blank4.x=130;
			blank4.y=320;
			movie1.addChild(blank4);
			
			blank4t.text = "4";
			blank4t.width=90;
			blank4t.height=50;
			blank4t.x=165;
			blank4t.y=350;
			blank4t.visible=true;
			blank4t.alpha=0.5;
			blank4t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank4t);
			
			blank5.graphics.lineStyle(3,0xffffff);
			blank5.graphics.drawRect(0,0,90,90);
			blank5.x=804;
			blank5.y=190;
			movie1.addChild(blank5);
			
			blank5t.text = "5";
			blank5t.width=90;
			blank5t.height=50;
			blank5t.x=839;
			blank5t.y=220;
			blank5t.visible=true;
			blank5t.alpha=0.5;
			blank5t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank5t);
			
			blank6.graphics.lineStyle(3,0xffffff);
			blank6.graphics.drawRect(0,0,90,90);
			blank6.x=914;
			blank6.y=190;
			movie1.addChild(blank6);
			
			blank6t.text = "6";
			blank6t.width=90;
			blank6t.height=50;
			blank6t.x=949;
			blank6t.y=220;
			blank6t.visible=true;
			blank6t.alpha=0.5;
			blank6t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank6t);
			
			blank7.graphics.lineStyle(3,0xffffff);
			blank7.graphics.drawRect(0,0,90,90);
			blank7.x=804;
			blank7.y=320;
			movie1.addChild(blank7);
			
			blank7t.text = "7";
			blank7t.width=90;
			blank7t.height=50;
			blank7t.x=839;
			blank7t.y=350;
			blank7t.visible=true;
			blank7t.alpha=0.5;
			blank7t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank7t);
			
			blank8.graphics.lineStyle(3,0xffffff);
			blank8.graphics.drawRect(0,0,90,90);
			blank8.x=914;
			blank8.y=320;
			movie1.addChild(blank8);
		
			blank8t.text = "8";
			blank8t.width=90;
			blank8t.height=50;
			blank8t.x=949;
			blank8t.y=350;
			blank8t.visible=true;
			blank8t.alpha=0.5;
			blank8t.setStyle("textFormat", myTextFormat2);
			movie1.addChild(blank8t);
			
			positionFixed[0]=blank1.x;
			positionFixed[1]=blank1.y;
			
			positionFixed[2]=blank2.x;
			positionFixed[3]=blank2.y;
			
			positionFixed[4]=blank3.x;
			positionFixed[5]=blank3.y;
			
			positionFixed[6]=blank4.x;
			positionFixed[7]=blank4.y;
			
			positionFixed[8]=blank5.x;
			positionFixed[9]=blank5.y;
			
			positionFixed[10]=blank6.x;
			positionFixed[11]=blank6.y;
			
			positionFixed[12]=blank7.x;
			positionFixed[13]=blank7.y;
			
			positionFixed[14]=blank8.x;
			positionFixed[15]=blank8.y;
			
			allLabels[0]=new Label();
			allLabels[0].width=110;
			allLabels[0].height=50;
			allLabels[0].x=blank1.x;
			allLabels[0].y=blank1.y+blank1.height+5;
			allLabels[0].visible=false;
			allLabels[0].alpha=0.5;
			allLabels[0].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[0]);
			
			allLabels[1]=new Label();
			allLabels[1].width=110;
			allLabels[1].height=50;
			allLabels[1].x=blank2.x;
			allLabels[1].y=blank2.y+blank2.height+5;
			allLabels[1].visible=false;
			allLabels[1].alpha=0.5;
			allLabels[1].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[1]);
			
			allLabels[2]=new Label();
			allLabels[2].width=110;
			allLabels[2].height=50;
			allLabels[2].x=blank3.x;
			allLabels[2].y=blank3.y+blank3.height+5;
			allLabels[2].visible=false;
			allLabels[2].alpha=0.5;
			allLabels[2].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[2]);
			
			allLabels[3]=new Label();
			allLabels[3].width=110;
			allLabels[3].height=50;
			allLabels[3].x=blank4.x;
			allLabels[3].y=blank4.y+blank4.height+5;
			allLabels[3].visible=false;
			allLabels[3].alpha=0.5;
			allLabels[3].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[3]);
			
			allLabels[4]=new Label();
			allLabels[4].width=110;
			allLabels[4].height=50;
			allLabels[4].x=blank5.x;
			allLabels[4].y=blank5.y+blank5.height+5;
			allLabels[4].visible=false;
			allLabels[4].alpha=0.5;
			allLabels[4].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[4]);
			
			allLabels[5]=new Label();
			allLabels[5].width=110;
			allLabels[5].height=50;
			allLabels[5].x=blank6.x;
			allLabels[5].y=blank6.y+blank6.height+5;
			allLabels[5].visible=false;
			allLabels[5].alpha=0.5;
			allLabels[5].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[5]);
			
			allLabels[6]=new Label();
			allLabels[6].width=110;
			allLabels[6].height=50;
			allLabels[6].x=blank7.x;
			allLabels[6].y=blank7.y+blank7.height+5;
			allLabels[6].visible=false;
			allLabels[6].alpha=0.5;
			allLabels[6].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[6]);
			
			allLabels[7]=new Label();
			allLabels[7].width=110;
			allLabels[7].height=50;
			allLabels[7].x=blank8.x;
			allLabels[7].y=blank8.y+blank8.height+5;
			allLabels[7].visible=false;
			allLabels[7].alpha=0.5;
			allLabels[7].setStyle("textFormat", myTextFormat2);
			movie1.addChild(allLabels[7]);
			
			
			right.load(new URLRequest("correct.png"));
			right.visible=false;
		
		
			wrong.load(new URLRequest("error.png"));
			wrong.visible=false;
		
			
			loadimages();
			
		}
		
		function loadimages()
		{
			var i:int=0;
			for(i=0; i<iLoaders.length; i++)
			{
				iLoaders[i] = new Loader();
				iLoaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImg);
				iLoaders[i].load(new URLRequest(pics[i]));
			}
		}
		
		function onLoadImg(evt:Event):void
		{
			if(once)
			{
			var i:int=0;
			for(i=0; i<iLoaders.length; i++)
			{
			
			var bitdata:BitmapData = new BitmapData(iLoaders[i].width, iLoaders[i].height,true, 0x00000000);
			bitdata.draw(iLoaders[i], new Matrix());
		
			var bitimage:Bitmap = new Bitmap(bitdata);
		
			bmpimages[i]=new tileT();
			bmpimages[i].name=i;
			
			bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);									
			bmpimages[i].addEventListener(TouchEvent.MOUSE_UP, this.upEvent);									
			
			bmpimages[i].addChild(bitimage);		
			movie1.addChild(bmpimages[i]);
			
			
			
			}
			
			addimages();
			}
			once=false;
		}
		
		var bound:Sprite=new Sprite();
		
		function addimages()
		{
			bound.graphics.lineStyle(3,0x000000);
			//bound.graphics.drawRect(230,340,560,360);  //for where the images are put
			//bound.graphics.drawRect(230,130,560,560);   //for dragging
			//movie1.addChild(bound);
			
			var i:int=0;
			var x1:Number = randomNumber(230,650);
			var y1:Number=550;
			var angle=0;
			var k=0;
			
			for(i=0; i<bmpimages.length; i++)
			{
				bmpimages[i].x=x1;
				bmpimages[i].y=randomNumber(340,560);
				bmpimages[i].rotationZ=angle;
				
				position[k]=bmpimages[i].x;
				k++;
				position[k]=bmpimages[i].y;
				k++;
				
				x1=randomNumber(230,650);
			}
		}
		
		function randomNumber(low:Number=NaN, high:Number=NaN):Number
		{
		  var low:Number = low;
		  var high:Number = high;
		
		  if(isNaN(low))
		  {
			throw new Error("low must be defined");
		  }
		  if(isNaN(high))
		  {
			throw new Error("high must be defined");
		  }
		
		  return Math.round(Math.random() * (high - low)) + low;
		}
		
		
		public function skip3(e:Event):void 
		{ 
			skip2=true; 
			movie1.removeChild(mainLoader); 
			if(mainLoader1.parent==movie1)
			{
				movie1.removeChild(mainLoader1);
				skipp1.visible=false;
				movie1.removeChild(skipp1);
			}
			//movie1.removeChild(mainLoader1);
			//movie1.removeChild(mainLoader2);
			skipp.visible=false;
			//skipp1.visible=false;
			movie1.removeChild(skipp);
			//movie1.removeChild(skipp1);
		}
		
		public function skip4(e:Event):void 
		{ 
			skip2=true; 
			
			movie1.removeChild(mainLoader1);
			skipp1.visible=false;
			movie1.removeChild(skipp1);
			//movie1.removeChild(mainLoader2);
			if(skipp.parent==movie1)
			{
			movie1.removeChild(mainLoader); 
			skipp.visible=false;
			movie1.removeChild(skipp);
			}
		
		}
		
		
		function closeApp(event:TouchEvent):void
		{
			
			fscommand("quit");
			//System.exit(0);
		
		}
		
		public function wellDone(e:Event):void
		{
			//myMaster.goToWellDone();
		}		
		
		function backMethod(event:TouchEvent):void
		{
			myMaster.backTopic();
		}
		
		function moveTip(event:TouchEvent):void{
			//if(mainLoader.visible==false)
			//{
				//backTip.enabled=true;
				//backTip.alpha=1;
				
				 mainLoader.addEventListener(TouchEvent.CLICK,downEvent);
				 var newEvent2:TouchEvent = new TouchEvent(TouchEvent.CLICK);
				 mainLoader.dispatchEvent(newEvent2);
			//}
		}
		
		function moveTipn(event:TouchEvent):void{
			//if(mainLoadern.visible==false)
			//{
				//backTip.enabled=true;
				//backTip.alpha=1;
				
				 mainLoadern.addEventListener(TouchEvent.CLICK,downEvent);
				 var newEvent2:TouchEvent = new TouchEvent(TouchEvent.CLICK);
				 mainLoadern.dispatchEvent(newEvent2);
			//}
		}
		
		function backTips(event:TouchEvent):void
		{
			if(enable==true)
			{
			var urln = new URLRequest("example4.swf"); 
			mainLoadern.load(urln);
			mainLoadern.y=470;  //10
			mainLoadern.x=20;  //852
			mainLoadern.addEventListener(TouchEvent.MOUSE_DOWN,moveTipn);
			movie1.addChild(mainLoadern);
			
			skipp.label="Skip All Tips";
			//skipp.addEventListener(TouchEvent.MOUSE_DOWN, skip3); 
			skipp.y=617;  //146
			skipp.x=38;
			skipp.width=130;
			skipp.name="hhh";
			skipp.visible=false;
			movie1.addChild(skipp);
			
			backTip.enabled=false;
			backTip.alpha=0.5;
			}
		}
		
		function muteMethod(event:TouchEvent):void
		{
			if(mute.name=='unmute')   // want to pause
			{
			//myMaster.mute();
			//mute2();
			noSound=true;
			mute.load(new URLRequest(images[3]));
			mute.name='mute';
			
			}
			else   // want to play again
			{
			//unmute2();
			noSound=false;
			mute.load(new URLRequest(images[4]));	
			mute.name='unmute';
			}
		}
		
		private function drawButtonState(rgb:uint):Sprite {
		var sprite:Sprite = new Sprite();
		//sprite.graphics.lineStyle(4,0x33621E,1);
		sprite.graphics.beginFill(rgb);
		sprite.graphics.drawRoundRect(((this.stage.
		stageWidth/2)-(numButtonWidth/2)),((this.
		stage.stageHeight/2)-(numButtonHeight/2)),
		numButtonWidth,numButtonHeight,10,10);
		return sprite;
		}
		
		function updater(e:Event):void {
            for (var i:int = 0; i < blobs.length; i++) { 
                var tuioobj:TUIOObject=TUIO.getObjectById(blobs[i].id);
 
                // if not found, then it must have died..
                if (! tuioobj) {
                    removeBlob(blobs[i].id);
                } else if (parent != null) {
 
                    lines["line"+blobs[i].id].graphics.lineTo(tuioobj.x, tuioobj.y);
                }
            }
        }
		
		public function checkImage(s:String, xc:int, yc:int)
		{
			var totalx:int=xc+64;
			var totaly:int=yc+64;
			
			if(totalx+20>411 && totalx-20<613 && totaly+20>129 && totaly-20<331)
			{
				trace("xc is"+xc+" and yc is "+yc);
			var index:int = parseInt(s);
			gIndex=index;
			trace("the x is "+bmpimages[index].x + " and the y is "+ bmpimages[index].y );
			backPosition=index*2;
			trace("GIndex");
			trace(gIndex);
			//var totalx:int=bmpimages[index].x+bmpimages[index].width;
			//var totaly:int=bmpimages[index].y+bmpimages[index].height;
			
			myTimerC.addEventListener(TimerEvent.TIMER, timerListenerC);
			myTimerI.addEventListener(TimerEvent.TIMER, timerListenerI);
			timerLast.addEventListener(TimerEvent.TIMER, timerlasts);
			
				if(index<8)
				{
					right.x=bmpimages[index].x;
					right.y=bmpimages[index].y;
					right.visible=true;
				
					movie1.addChild(right);
					
					bigName.text=names[index];
					movie1.addChild(bigName);
					bigName.visible=true;
					
					if(noSound==false)
					{
					correctSound= new Sound();
					correctSound.load(new URLRequest("woho.mp3"));
					correctSound.play();
					}
					
					myTimerC.start();
					pleft--;
				}
				else
				{
					wrong.x=bmpimages[index].x;
					wrong.y=bmpimages[index].y;
					wrong.visible=true;
					movie1.addChild(wrong);
					
					bigName.text=names[index];
					movie1.addChild(bigName);
					bigName.visible=true;
					
					if(noSound==false)
					{
					errorSound= new Sound();
					errorSound.load(new URLRequest("error.mp3"));
					errorSound.play();
					}
					
					myTimerI.start();
				}
			
			}
		}
		
		function timerListenerC (e:TimerEvent):void
		{
			trace("Timer is Triggered");
			right.visible=false;
			bigName.visible=false;
			
			bmpimages[gIndex].x=positionFixed[pdone];
			pdone++;
			bmpimages[gIndex].y=positionFixed[pdone];
			pdone++;
			bmpimages[gIndex].scaleX=0.7;
			bmpimages[gIndex].scaleY=0.7;
			
			allLabels[pdone2].text=names[gIndex];
			allLabels[pdone2].visible=true;
			
			pdone2++;
			
			myTimerC.stop();
			
			
			if(pleft==0)
				{
					timerLast.start();
				}
		}
		
		function timerListenerI (e:TimerEvent):void{
		
			wrong.visible=false;
			bigName.visible=false;
			
			bmpimages[gIndex].x=position[backPosition];
			backPosition++;
			bmpimages[gIndex].y=position[backPosition];
			myTimerI.stop();
			//bmpimages[gIndex].visible=false;
		}
		
		function timerlasts (e:TimerEvent):void{
			timerLast.stop();
			myMaster.goToWellDone();
		}
		
		 public function downEvent(e:TouchEvent):void {
			/* if(skip2==false)
			{
			mainLoader1.visible=true;
			skipp1.visible=true;
			}*/
			
			if(skipp.parent!=movie1)
			{
			backTip.enabled=true;
			backTip.alpha=1.0;
			enable=true;
			}
			
			if(e.stageX == 0 && e.stageY == 0)
				return;			
			
			var curPt:Point = parent.globalToLocal(new Point(e.stageX, e.stageY));									
			

			addBlob(e.ID, curPt.x, curPt.y);
			
			if(bringToFront)
				this.parent.setChildIndex(this, this.parent.numChildren-1);
			
				if(mouseSelection)
				{
					//this.alpha=0.5;				
				}
			
			//DoubleTap Gesture
			var tuioobj:TUIOObject = TUIO.getObjectById(e.ID);
			var localPt:Point = globalToLocal(new Point(tuioobj.x, tuioobj.y));
			
			xdist = localPt.x - oldX;
			ydist = localPt.y - oldY;			
			distance = Math.sqrt(xdist*xdist+ydist*ydist);
			oldX = localPt.x;
			oldY = localPt.y;		
			
			//trace(distance);
			
			if (parent.hitTestPoint(localPt.x,localPt.y) && distance <= clickRadius ) {
				
				checkImage(e.currentTarget.name, e.currentTarget.x, e.currentTarget.y);

				/*if (lastClick == 0) {
					lastClick = getTimer();
				} else {
					lastClick = 0;
					trace("Double Tap");
					
				}*/
			}//end DoubleTap
				
			//e.stopPropagation();
			
		 }
		
		function clickHandler(evtObj:MouseEvent){
		trace("The " + evtObj.target.label + " button was clicked");
		}
 
        // When a finger stops touching, call removeBlob
        public function upEvent(e:TouchEvent):void {
			
			
			checkImage(e.currentTarget.name, e.currentTarget.x, e.currentTarget.y);
			
            removeBlob(e.ID);
            e.stopPropagation();
        }
		
		 function addBlob(id:Number, origX:Number, origY:Number):void {
            blobs.push( {id: id, origX: origX, origY: origY, myOrigX: x, myOrigY:y} );
 
            var line:Sprite = new Sprite();
            lines["line"+id] = line;
            //lines["line"+id].graphics.lineStyle(thickness, color); //<-- config above
            lines["line"+id].graphics.moveTo(origX, origY); //starting point of line to be drawn
            addChild(lines["line"+id]); // add line to stage
        }
 
 		function removeBlob(id:Number):void {
            for (var i=0; i < blobs.length; i++) {
                if (blobs[i].id==id) {
                    blobs.splice(i, 1);
                    return;
                }
            }
		  }
		
		
		
		
	  }
	  
	  
}
		
		